// pages/Users/UserTeams/UserTeams.js
//获取应用实例
const app = getApp();
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { getGradeConfig } from '../../../api/brand';
import { getUserSonInfo , getUserTeamNumber , getUserParent } from '../../../api/user';
import { throttle } from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shareTitle: app.globalData.global.shareTitle,
    navBgColor: 'linear-gradient(to right,rgb(20, 172, 105),rgb(62, 214, 147))',
    tabNavTop: app.globalData.CustomBar,
    assetsAddress: assetsAddress,
    userName: '', // 下级用户名称
    gradeList: [],
    show: false,
    page: 0,
    loading: true,
    noMore: false,
    loadingFailed: false,
    isReachBottom: false,
    dataList: [],
    todayDirectCount: 0, // 今日新增直推人数
    totalDirectCount: 0, // 总直推人数
    totalCount: 0, // 团队总数
    parentUser: {}, // 上级信息
    paramsList: [
      { id: '0', label: '注册时间' , data: 'createTime'},
      { id: '1', label: '等级' , data: 'grade'},
      { id: '2', label: '订单总量' , data: 'buyCount'},
      { id: '3', label: '购买总量' , data: 'teamBuyCount'},
      { id: '4', label: '团队人数' , data: 'teamCount'},
    ], // 筛选条件
    grade: '', // 等级
    sort: '',
    order: '',
    account: '', // 手机号
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getGradeConfig();
    this._getUserTeam();
    this._getUserTeamNumber();
    this._getUserParent();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 0,
      isReachBottom: false,
      noMore: false,
      loading: false,
      loadingFailed: false,
      grade: '',
      sort: '',
      order: '',
      account: '',
    })
    this.getGradeConfig();
    this._getUserTeam();
    this._getUserTeamNumber();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1000)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: throttle(function(){
    console.log('上拉加载')
    if(this.data.isReachBottom){
      this._getUserTeam()
    }
  },500),

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId')
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  // 返回上一页
  back(){
    wx.navigateBack({
      delta: 1,
    })
  },
  // 获取用户团队数据
  _getUserTeam(){
    let _this = this;
    getUserSonInfo({grade: _this.data.grade,account: _this.data.account,sort: _this.data.sort,order: _this.data.order,page: _this.data.page})
    .then(res => {
      if(res.success){
        // _this.setData({
        //   dataList: res.data,
        //   isReachBottom: false,
        //   noMore: true,
        //   loading: false,
        //   loadingFailed: false,
        // })
        var list = [];
        if(_this.data.page == 0){
          list = res.data;
        }else{
          list = _this.data.dataList;
          list = list.concat(res.data);
        }
        _this.setData({
          dataList: list,
          // total: res.data.totalElements,
          page: _this.data.page+1,
        })
        if(res.data.length == 0){
          _this.setData({
            isReachBottom: false,
            noMore: true,
            loading: false,
            loadingFailed: false,
          })
        }else{
          _this.setData({
            isReachBottom: true,
            noMore: false,
            loading: false,
            loadingFailed: false,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 展示等级选择
  getGradeConfig(){
    let _this = this;
    getGradeConfig()
    .then(res => {
      if(res.success){
        _this.setData({
          gradeList: res.data.content.concat({id: '', grade: '', gradeName: '全部'})
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  showSelGrade(){
    this.setData({
      show: true,
    })
  },
  selGrade(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    console.log(item,'item')
    this.setData({
      show: false,
      page: 0,
      grade: item.grade,
      gradeName: item.gradeName,
    })
  },
  onClose(){
    this.setData({
      show: false,
    })
  },
  // 获取团队人数统计
  _getUserTeamNumber(){
    let _this = this;
    getUserTeamNumber()
    .then(res => {
      if(res.success){
        _this.setData({
          todayDirectCount: res.data.todayDirectCount,
          totalDirectCount: res.data.totalDirectCount,
          totalCount: res.data.totalCount,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 获取上级信息
  _getUserParent(){
    let _this = this;
    getUserParent()
    .then(res => {
      if(res.success){
        _this.setData({
          parentUser: res.data,
        })
      }else{
        if(res.errMessage != '无上级'){
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      }
    })
  },
  // 拨打电话
  call(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    wx.makePhoneCall({
      phoneNumber: item,
    })
  },
  // 条件筛选排序
  selParams(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    if(item.data != this.data.sort){
      this.setData({
        sort: item.data,
        order: 'ASC',
      })
    }else{
      this.setData({
        order: this.data.order == 'ASC' ? 'DESC' : 'ASC'
      })
    }
    this._getUserTeam();
  },
})